package com.azier.kapacitor.script.method;

import com.azier.kapacitor.script.arg.Arg;
import com.azier.kapacitor.util.CollectionUtil;

import java.util.List;
import java.util.Objects;

/**
 * 1 * @Author: luoj
 * 2 * @Date: 2019/11/8 11:27
 * 3
 */

public abstract class AbstractMethod implements Method {
    final String name;

    final List<Arg> args;

    protected AbstractMethod(List<Arg> args,String name) {
        this.args = args;
        this.name = name;
    }
    @Override
    public StringBuilder append(StringBuilder stringBuilder){
        stringBuilder.append(".").append(name).append("(");
        if (CollectionUtil.isNotEmpty(args)) {
            for (int i = 0; i < args.size(); i++) {
                args.get(i).append(stringBuilder);
                if(i!=args.size()-1){
                    stringBuilder.append(",");
                }
            }
        }
        stringBuilder.append(")");
        return stringBuilder;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AbstractMethod that = (AbstractMethod) o;
        return name.equals(that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}
